package com.pxst.reconciliation.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pxst.earlywarn.entity.SysEarlywarnEntity;
import com.pxst.earlywarn.service.SysEarlywarnService;
import com.pxst.enums.AmqEnum;
import com.pxst.plugins.entity.PluginsEntity;
import com.pxst.plugins.service.PluginsService;
import com.pxst.reconciliation.req.ReconciliationReq;
import com.pxst.reconciliation.service.ReconciliationService;
import com.pxst.utils.AmqUtils;
import com.pxst.utils.PlugsUtils;

/**
 * @author yangxy
 * @version 创建时间：2023年9月4日 下午6:03:29
 */
@Service
public class ReconciliationServiceImpl implements ReconciliationService {
	@Autowired
	private PluginsService pluginsService;
	@Autowired
	private PlugsUtils plugsUtils;
	@Autowired
	private SysEarlywarnService sysEarlywarnService;
	@Autowired
	private AmqUtils amqUtils;
	
	@Override
	public void reconciliation(ReconciliationReq reconciliationReq) {
		// TODO Auto-generated method stub
		PluginsEntity pluginsEntity = pluginsService.getOne(new QueryWrapper<PluginsEntity>().lambda().eq(PluginsEntity::getBankName,
				reconciliationReq.getBankname()).eq(PluginsEntity::getStatus, 0).eq(PluginsEntity::getPlugsType, 1));
		
		try {
			boolean plugsExec = (boolean) plugsUtils.plugsExec(pluginsEntity.getBeanName(), pluginsEntity.getRunMethod(), Lists.newArrayList(JSON.toJSONString(reconciliationReq)));
			if(plugsExec) {
				return;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			SysEarlywarnEntity sysEarlywarnEntity = new SysEarlywarnEntity();
			sysEarlywarnEntity.setContent("对账失败");
			sysEarlywarnEntity.setErrMsg(e.getMessage());
			sysEarlywarnService.save(sysEarlywarnEntity);
		}
		
		//通知迅银捞帐参数(判断token过期时间，如果过期不再发送消息)
		Map<String,Object> paramMap = Maps.newHashMap();
		amqUtils.sendDelayedMsg(AmqEnum.APP_SALVAGE_ACCOUNT.exchange, AmqEnum.APP_SALVAGE_ACCOUNT.routingKey, paramMap, 10);
	}

}
